﻿using KFC.Model;
using KFC.MonAnF;
using KFC.MyUserControl;
using KFC.ThanhVienF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KFC
{
    public partial class Form1 : Form
    {
        private List<MonAn> danhSachMonAn;
        private List<MonAn> danhSachMonSeMua;
        MonAnFactory monAnFactory = new MonAnFactory();
        QuanLyMonAn quanLyMonAn = QuanLyMonAn.Instance();
        ServiceReferenceKFC.Service1SoapClient client
             = new ServiceReferenceKFC.Service1SoapClient();
        private List<ThanhVien> danhSachThanhVien
            = new List<ThanhVien>();
        private HoaDon hd;

        public Form1()
        {
            InitializeComponent();
            danhSachMonSeMua = new List<MonAn>();
            hd = new HoaDon();
            monAnFactory.KhoiTaoDSLoaiMonAn();
            monAnFactory.KhoiTaoDanhSachMonAn();
            danhSachMonAn = monAnFactory.LayDSMonAn();

            //load user control
            ThemMonAnUSC tmausc = ThemMonAnUSC.Instance(this);
            tmausc.Location = new System.Drawing.Point(690, 110);
            this.tabPage2.Controls.Add(tmausc);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDSMonAn(listView_DSMonAn);
        }

        private void LoadDSMonAn(ListView lv)
        {
            lv.Items.Clear();
            for (int i = 0; i < QuanLyMonAn.dsMonAn.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(QuanLyMonAn.dsMonAn[i].TenMonAn);
                lvi.SubItems.Add(QuanLyMonAn.dsMonAn[i].DonGia.ToString());
                lvi.SubItems.Add(QuanLyMonAn.dsMonAn[i].ChiTietMonAn);
                lv.Items.Add(lvi);
                comboBox_monAn.Items.Add(QuanLyMonAn.dsMonAn[i].TenMonAn);
            }
        }
            
       
            
        private void button_Mua_Click(object sender, EventArgs e)
        {
            if (comboBox_monAn.SelectedItem != null && textBox_soLuong.Text != "")
            {
                String ten_mon_an = comboBox_monAn.SelectedItem.ToString();
                
                int so_luong;
                try
                {
                    so_luong = int.Parse(textBox_soLuong.Text);
                }
                catch
                {
                    MessageBox.Show("So luong nhap khong dung!", "Thong bao");
                    return;
                }

                MonAn monAn = (from m in danhSachMonAn
                              where m.TenMonAn == ten_mon_an
                              select m).ToList()[0];
                hd.ThemMonAn(monAn, so_luong);
                Refesh();
            }
        }

        private void Refesh()
        {
            listView_DatMua.Items.Clear();
            for (int i = 0; i < hd.DanhSachMonAn.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(hd.DanhSachMonAn[i].TenMonAn);
                lvi.SubItems.Add(hd.DanhSachSoLuong[i].ToString());
                int t = hd.DanhSachMonAn[i].DonGia * hd.DanhSachSoLuong[i];
                lvi.SubItems.Add(t.ToString());
                
                listView_DatMua.Items.Add(lvi);
            }

            label3.Text = "Tong Tien: " + hd.TinhTien().ToString();
        }

        private void listView_DatMua_SelectedIndexChanged(object sender, EventArgs e)
        {        
        }
        private void listView_DSMonAn_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string tenMonAn = listView_DSMonAn.Text;
            comboBox_monAn.Text = tenMonAn;
        }

        private void button_TTThanhVien_Click(object sender, EventArgs e)
        {
            MaThe m = new MaThe();
            m.ShowDialog();
            int maThe = m.ID;
            ThongBaoTienPhaiTra(maThe);
            XoaHoaDon();
        }

        private void button_TTKhacHangThuong_Click(object sender, EventArgs e)
        {
            ThongBaoTienPhaiTra(-1);
            XoaHoaDon();
        }

        private void ThongBaoTienPhaiTra(int maThe)
        {
            string tien = hd.TinhTienHoaDon(maThe).ToString();
            MessageBox.Show("Số tiền khách hàng phải trả: " + tien);
        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {
            RefershListViewDSMonAn();

        }

        public void RefershListViewDSMonAn()
        {
            listViewDSMonAn.Items.Clear();
            for (int i = 0; i < QuanLyMonAn.dsMonAn.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(QuanLyMonAn.dsMonAn[i].MaMonAn);
                lvi.SubItems.Add(QuanLyMonAn.dsMonAn[i].TenMonAn);
                lvi.SubItems.Add(QuanLyMonAn.dsMonAn[i].DonGia.ToString());
                lvi.SubItems.Add(QuanLyMonAn.dsMonAn[i].ChiTietMonAn);
                listViewDSMonAn.Items.Add(lvi);
            }
        }

        private void buttonThem_Click(object sender, EventArgs e)
        {
            ChonLoaiPhanAnForm chonLoaiPhanAn = new ChonLoaiPhanAnForm();
            chonLoaiPhanAn.ShowDialog();
            MonAn monAn = chonLoaiPhanAn.MonAn;
            ThemMonAnUSC tmausc = ThemMonAnUSC.Instance(this);
            tmausc.InitAll(monAn);
            RefershListViewDSMonAn();
        }

        private void listViewDSMonAn_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonXoa_Click(object sender, EventArgs e)
        {
            string maMonAn;
            try
            {
                 maMonAn = listViewDSMonAn.SelectedItems[0].Text;
            }
            catch
            {
                MessageBox.Show("Chon mon can xoa");
                return;
            }
            

            DialogResult dr =
            MessageBox.Show("Ban co thuc su muon xoa mon an " + maMonAn,
                "Thong bao",MessageBoxButtons.OKCancel);

            List<MonAn> l = danhSachMonAn;
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                
                quanLyMonAn.HuyMon(maMonAn);
            }
            this.RefershListViewDSMonAn();
        }

        private void tabPage1_Enter(object sender, EventArgs e)
        {
            LoadDSMonAn(listView_DSMonAn);
        }

        private void listView_DSMonAn_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView_DSMonAn.SelectedItems.Count != 0)
                comboBox_monAn.Text = listView_DSMonAn.SelectedItems[0].Text;

            
        }

        private void tabPage3_Enter(object sender, EventArgs e)
        {
            LoadDSThanhVien();
            
        }

        private void LoadDSThanhVien()
        {
            danhSachThanhVien = new List<ThanhVien>();
            var l = client.LayDSThanhVien();
            foreach (var i in l)
            {
                ThanhVien t = new ThanhVien();
                t.MaThe = i.TheID;
                t.GiamGia = i.PhanTramGiamGia;
                danhSachThanhVien.Add(t);
            }

            listViewDSThanhVien.Items.Clear();
            for (int i = 0; i < danhSachThanhVien.Count; i++)
            {
                ListViewItem lvi = new ListViewItem(danhSachThanhVien[i].MaThe.ToString());
                lvi.SubItems.Add(danhSachThanhVien[i].GiamGia.ToString());
                listViewDSThanhVien.Items.Add(lvi);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int k = client.ThemThanhVien();
            MessageBox.Show("Thêm thành công! Ma so the la: " + k.ToString(),
                "Thong bao");
            LoadDSThanhVien();

        }

        private void buttonLoadXML_Click(object sender, EventArgs e)
        {
            LoadMonAnFromXML l = new LoadMonAnFromXML();
            l.ShowDialog();
            RefershListViewDSMonAn();
        }

        private void button_xoa_Click(object sender, EventArgs e)
        {
            XoaHoaDon();
        }

        private void XoaHoaDon()
        {
            listView_DatMua.Items.Clear();
            hd.DanhSachMonAn.Clear();
            hd.DanhSachSoLuong.Clear();
            Refesh();
        }

        private void button_CapNhat_Click(object sender, EventArgs e)
        {
            CapNhatGiaForm cngf = new CapNhatGiaForm(this);
            cngf.ShowDialog();
        }
    }
}
